package org.sintec.AndroidCarConnectivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class activityPreferences extends PreferenceActivity
{
	protected static final int REQUEST_CONNECT_DEVICE = 0;
	
	@Override
    protected void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        // Get the custom preference
        
        Preference customPref = (Preference) findPreference("pref_bluetooth_address");
        customPref.setOnPreferenceClickListener(new OnPreferenceClickListener()
        {
            public boolean onPreferenceClick(Preference preference)
            {
            	Intent serverIntent = new Intent(activityPreferences.this, activityDeviceList.class);
                startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
                
                return true;
            }
        });
    }
	
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    	if (requestCode == REQUEST_CONNECT_DEVICE)
    	{
    		if (resultCode == RESULT_OK)
    		{
                String address = data.getStringExtra(activityDeviceList.EXTRA_DEVICE_ADDRESS);
                
		        savePreference("pref_bluetooth_address", address);
    		}
    	}
    }
    
    public void savePreference(String key, String value)
    {
    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key, value);
        editor.commit();
    }
}
